#pragma once
#include "compiler.hpp"
#include "runner.hpp"
#include "../comm/log.hpp"
#include "../comm/util.hpp"

namespace ns_compile_run
{
    using namespace ns_compiler;
    using namespace ns_run;
    using namespace ns_log;
    using namespace ns_util;

    class CompileAndRun
    {
        /***************************
         * in_json:{code:#include<>....}{input: }{cpu_limit:1}{mem_limit:10240}
         * out_json:{"status":"0", "reason":"","stdout":"","stderr":"",}
         *****************************/
    public:
        static void reMoveFiles(const std::string &file_name)
        {
            std::string src = PathUtil::scr(file_name);
            if (FileUtil::isFileExists(src))
                unlink(src.c_str());

            std::string compile_err = PathUtil::compileErr(file_name);
            if (FileUtil::isFileExists(compile_err))
                unlink(compile_err.c_str());

            std::string exe = PathUtil::exe(file_name);
            if (FileUtil::isFileExists(exe))
                unlink(exe.c_str());

            std::string _stdin = PathUtil::stdIn(file_name);
            if (FileUtil::isFileExists(_stdin))
                unlink(_stdin.c_str());

            std::string _stdout = PathUtil::stdOut(file_name);
            if (FileUtil::isFileExists(_stdout))
                unlink(_stdout.c_str());

            std::string _stderr = PathUtil::stdErr(file_name);
            if (FileUtil::isFileExists(_stderr))
                unlink(_stderr.c_str());
        }
        static std::string codeToDesc(int code, const std::string &file_name)
        {
            std::string reason;
            switch (code)
            {
            case 0:
                reason = "代码运行正常";
                break;
            case -1:
                reason = "代码内容为空";
                break;
            case -2:
                reason = "未知错误";
                break;
            case -3:
                FileUtil::readFile(PathUtil::compileErr(file_name), &reason, true);
                break;
            case SIGABRT: // 6
                reason = "内存超过范围";
                break;
            case SIGXCPU: // 24
                reason = "CPU使用超时";
                break;
            case SIGFPE: // 8
                reason = "浮点数溢出";
                break;
            default:
                reason = "未知: " + std::to_string(code);
                break;
            }
            return reason;
        }
        static void start(const std::string &in_json, std::string *out_json)
        {
            Json::Value in;
            JsonUtil::DeSerialize(in_json, in);

            std::string code = in["code"].asString();
            std::string input = in["input"].asString();
            int cpu_limit = in["cpu_limit"].asInt();
            int mem_limit = in["mem_limit"].asInt();

            int status_code = 0;
            int run_code = 0;
            Json::Value out;
            std::string file_name;

            if (code.empty())
            {
                // 代码内容为空
                status_code = -1;
                goto END;
            }

            // 生成唯一文件名
            file_name = FileUtil::getFileName();

            // 形成src文件
            if (!FileUtil::writeFile(PathUtil::scr(file_name), code))
            {
                // 未知错误
                status_code = -2;
                goto END;
            }
            if (!compiler::compile(file_name))
            {
                // 编译失败
                status_code = -3;
                goto END;
            }
            run_code = Runner::Run(file_name, cpu_limit, mem_limit);
            if (run_code < 0)
            {
                status_code = -2;
                // 内部未知错误
            }
            else if (run_code == 0)
            {
                status_code = 0;
            }
            else
            {
                status_code = run_code;
            }
        END:
            out["status"] = status_code;
            out["reason"] = codeToDesc(status_code, file_name);

            if (status_code == 0)
            {
                // 编译运行成功
                std::string _stdout;
                FileUtil::readFile(PathUtil::stdOut(file_name), &_stdout, true);
                out["stdout"] = _stdout;

                std::string _stderr;
                FileUtil::readFile(PathUtil::stdErr(file_name), &_stderr, true);
                out["stderr"] = _stderr;
            }
            JsonUtil::Serialize(out, *out_json);
            reMoveFiles(file_name);
        }
    };

}
